package com.shopping.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.common.entity.GoodSecondSkill;
import com.shopping.common.entity.vo.GoodSecondSkillVo;
import com.shopping.common.entity.vo.UserVo;
import com.shopping.common.result.Result;
import com.shopping.common.utils.StringUtils;
import com.shopping.system.service.SystemService;
import com.shopping.system.service.impl.InformationImpl;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/information")
@Log4j2
public class InformationController {

    @Autowired
    SystemService systemService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 查找此时段已经开始的秒杀商品
     * @return
     */
    @PostMapping("/findVoliadSecondSkillGood")
    public Result<List<GoodSecondSkillVo>> findVoliadSecondSkillGood(){
        log.info("请求路径：【{}】，请求方式：【{}】",
                request.getRequestURI(),request.getMethod());
        Result<List<GoodSecondSkillVo>> result = systemService.findVoliadSecondSkillGood();

        log.info("请求路径：【{}】，请求方式：【{}】,响应结果:【{}】",
                request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 查找商品详细信息
     * @return
     */
    @PostMapping("/findGoodMore/{id}")
    public Result<GoodSecondSkillVo> findGoodMore(@PathVariable String id){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(),id);
        Result<GoodSecondSkillVo> result = systemService.findGoodMore(id);
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /***
     * 查找查寻过的商品
     * @return
     */
    @PostMapping("/findIsGood")
    public Result<Integer> findIsGood(){
        log.info("请求路径：【{}】，请求方式：【{}】",
                request.getRequestURI(),request.getMethod());
        Result<Integer> result = null;
        UserVo userLogin = InformationImpl.findUserLogin(redisTemplate, request);
        String s = (String) redisTemplate.opsForValue().get("urlKey:" + userLogin.getId());
        if(StringUtils.isEmpty(s)) {
            result = Result.success(0);
            log.info("请求路径：【{}】，请求方式：【{}】,响应结果：【{}】",
                    request.getRequestURI(),request.getMethod(),result);
            return result;
        }
        redisTemplate.delete("urlKey:"+userLogin.getId());
        String[] split = s.split("/#");
        String s1 = split[split.length - 1];
        result = Result.success(Integer.parseInt(s1));
        log.info("请求路径：【{}】，请求方式：【{}】,响应结果：【{}】",
                request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 添加商品
     * @return
     */
    @PostMapping("/addGood")
    public Result<String> addGood(){

        return null;
    }

    @PostMapping("/updateGood")
    public Result<String> updateGood(){

        return null;
    }

    /**
     * 查找本日剩余时间段
     * @return
     */
    @PostMapping("/findTimeCount")
    public Result<List<String>> findTimeCount(){

        log.info("请求路径：【{}】，请求方式：【{}】",
                request.getRequestURI(),request.getMethod());
        ArrayList<String> strings = new ArrayList<>();

        // 当前时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH");
        String format1 = format.format(date);

        // 所有的小时
        for (int i = 0; i < 24; i++) {
            // 剩余的小时
           if(i>=Integer.parseInt(format1)){
               if(i == 0) {
                   strings.add(i+"0");
               }else{
                   strings.add(i+"");
               }
           }
        }
        Result<List<String>> result = Result.success(strings);
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果:【{}】",
                request.getRequestURI(),request.getMethod(),result);
        return result;
    }

    /**
     * 添加购物车
     * @param goodSecondSkillVo
     * @return
     */
    @PostMapping("/addShopCar")
    public Result<String> addShopCar(@RequestBody GoodSecondSkillVo goodSecondSkillVo){
        log.info("请求路径：【{}】，请求方式：【{}】，请求参数：【{}】",
                request.getRequestURI(),request.getMethod(), JSONObject.toJSONString(goodSecondSkillVo));
        Result<String> result = null;
        UserVo userLogin = InformationImpl.findUserLogin(redisTemplate, request);
        // 是否已加入该商品
        Integer count = systemService.findIsAddGood(goodSecondSkillVo.getGoodSkuChecked());
        // 已加入过该商品
        if(count>0){
            systemService.addShopCarUpdateNum(goodSecondSkillVo.getGoodSkuChecked(),goodSecondSkillVo.getBuyNum());
            result = Result.success("加入成功");

            log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                    request.getRequestURI(),request.getMethod(),result);
            return result;
        }
        // 首次加入
        systemService.addShopCar(goodSecondSkillVo.getBuyNum(),goodSecondSkillVo.getGoodSkuChecked(),userLogin.getId());

        result = Result.success("加入成功");
        log.info("请求路径：【{}】，请求方式：【{}】，响应结果：【{}】",
                request.getRequestURI(),request.getMethod(),result);
        return result;
    }
}
